package com.jsyso.weixin.api;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.jsyso.jsyso.json.jackson.JsonMapper;
import com.jsyso.jsyso.lang.JStringUtils;
import com.jsyso.jsyso.util.JsMap;

/**
 * 多客服功能相关接口
 * @author janjan
 *
 */
public class MessageCustomAPI extends API {
	public static enum MessageCustomType {
		TEXT, IMAGE, VOICE, VIDEO, MUSIC, NEWS, MPNEWS, WXCARD;
        public String get() {
            return this.name().toLowerCase();
        }
    }
	
	protected static String CUSTOM_SEND = "/cgi-bin/message/custom/send?access_token=";
	
	public API send(String appid, String touser, JsMap params, MessageCustomType messageType) {
		JsMap data = JsMap.create("touser", touser).set(messageType.get(), params)
				.set("msgtype", messageType.get());
		// 获取token
		JsMap resMap = AccessTokenAPI.getAccessToken(appid);
		String accessToken = resMap.get("access_token", String.class);
		if(StringUtils.isNotBlank(accessToken)) {
			this.url = JStringUtils.newBuilder(API_WEIXIN_QQ_COM, CUSTOM_SEND, accessToken).toString();
			this.postData = JsonMapper.getInstance().toJson(data);
			this.method = RequestMethod.POST;
		}
		if(logger.isDebugEnabled()) {
			logger.debug("[发送客服消息]postData=" + (this.postData));
		}
		return this;
	}
	
	public API sendImage(String appid, String touser, String mediaId) {
		return send(appid, touser, JsMap.create("media_id", mediaId), MessageCustomType.IMAGE);
	}
	
	public API sendNews(String appid, String touser, List<JsMap> articles) {
		return send(appid, touser, JsMap.create("articles", articles), MessageCustomType.NEWS);
	}
	
	public API sendMpNews(String appid, String touser, String mediaId) {
		return send(appid, touser, JsMap.create("media_id", mediaId), MessageCustomType.MPNEWS);
	}
}
